package com.vito.framework.system.user.service.impl;

import com.vito.framework.base.exception.CommonException;
import com.vito.framework.base.service.impl.BaseServiceImpl;
import com.vito.framework.system.role.model.RoleBean;
import com.vito.framework.system.user.mapper.UserMapper;
import com.vito.framework.system.user.model.UserBean;
import com.vito.framework.system.user.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserBean> implements UserService {

	@Resource
	UserMapper userMapper;

	@Override
	public List<UserBean> getUserList(Map<String, Object> paramMap) throws CommonException {
        return userMapper.getUserList(paramMap);
	}

	@Override
	public UserBean getUserById(Map<String, Object> paramMap) throws CommonException {
		UserBean userBean = userMapper.getUserById(paramMap);
	    if (userBean != null && userBean.getRoleIdsList() != null)
	        userBean.setRoleIds(userBean.getRoleIdsList().toArray(new String[userBean.getRoleIdsList().size()]));
        return userBean;
	}
	
	@Override
	public List<RoleBean> getRoleByOrgId(String orgId) throws CommonException {
		return userMapper.getRoleByOrgId(orgId);
	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void addUser(UserBean user) throws CommonException {
			userMapper.insertUser(user);

			this.setRole(user);
        	if(user.getRoleSet() !=null ){
	        	for(RoleBean role:user.getRoleSet()){
	        		userMapper.insertUserRole(role);
	        	} 
        	}
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editUser(UserBean user) throws CommonException {
		String count = "0";
       	String account = user.getAccount();
       	String oldAccount = user.getOldAccount();
		if (!account.equals(oldAccount)){
			count = userMapper.getUserEditCount(user);
		}
		if(("0").equals(count)) {
			this.setRole(user);
			userMapper.updateUser(user);
			userMapper.deleteUserRole(user.getUserId());
        	if(user.getRoleSet() !=null ){
	        	for(RoleBean role:user.getRoleSet()){
	        		userMapper.insertUserRole(role);
	        	} 
        	}
		}
	}
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void deleteUser(Map<String, Object> paramMap) throws CommonException {
		String[] ids = (String[])paramMap.get("ids");
    	if(ids ==null || ids.length<1){
    		throw new CommonException("No data to operate");
    	}
    	userMapper.deleteUser(paramMap);
    	for(String id:ids){
    		userMapper.deleteUserRole(id);
    	}
	}
	/**
	 * Author:zhaorui
     * Create At:2010-06-29
     * Description: [用户管理-暂停用户信息]
     * @return String
     */
	@Override
	public void pauseUser(Map<String, Object> paramMap)throws CommonException {
		userMapper.pauseUserStat(paramMap);
	}
	/**
	 * Author:zhaorui
     * Create At:2010-06-29
     * Description: [用户管理-启动用户信息]
     * @return String
     */
	@Override
	public void reStartUser(Map<String, Object> paramMap)throws CommonException {
		userMapper.startUserStat(paramMap);
	}

	/**
	 * Author:zhaowentao
     * Create At:2010-06-29
     * Description: [用户修改个人资料]
     */
	@Override
	public void savePresonInfo(UserBean userBean) throws CommonException {
		Map<String,String> paramMap = new HashMap<String,String>();
		paramMap.put("orgId", userBean.getOrgId());
		paramMap.put("userId", userBean.getUserId());
		paramMap.put("oldPassword", userBean.getOldPassword());
		String flag = userMapper.checkOldPwd(paramMap);
		if("1".equals(flag)){
			userMapper.personInfo(userBean);
		}else{
			throw new CommonException("输入的原密码不正确");
		}
	}

	@Override
	public Integer updatePassword(Map<String, Object> paramMap)throws CommonException {
		return userMapper.updatePassword(paramMap);
	}
	/**
	 * 置入角色
	 * @param user
	 */
	
	private void setRole(UserBean user) {
		String[] ids =user.getRoleIds();
		if(ids != null){
			List<RoleBean> roleSet = new ArrayList<RoleBean>();
			for(String id:ids){
				RoleBean role = new RoleBean();
				role.setRoleId(id);
				role.setUserId(user.getUserId());
				roleSet.add(role);
			}
			user.setRoleSet(roleSet);
		}
		
	}
	@Override
	public String queryTemplateList(Map<String, Object> map)throws CommonException {
		return userMapper.queryTemplateList(map);
	}
	
	@Override
	public UserBean getUserByName(Map<String, Object> paramMap) throws CommonException {
        return userMapper.getUserByName(paramMap);
	}

	@Override
	public UserBean getUserByuserid(String userId) throws CommonException {
		return userMapper.selectbyuserid(userId);
	}
	@Override
	public void editGly(UserBean userbean) throws CommonException {
		userMapper.updategly(userbean);
		
	}

	@Override
	public UserBean getLoginUserById(Map<String, Object> paramMap) {
		paramMap.put("userId", paramMap.get("cur_userId"));
		UserBean userBean = userMapper.getUserById(paramMap);
		if (userBean != null && userBean.getRoleIdsList() != null)
			userBean.setRoleIds(userBean.getRoleIdsList().toArray(new String[userBean.getRoleIdsList().size()]));
		return userBean;
	}
}
